package itbour.onetouchshow.base;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.view.View;
import android.widget.ImageView;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.XRefreshViewFooter;
import com.lsjr.utils.NetUtils;

import butterknife.BindView;
import itbour.onetouchshow.R;
import itbour.onetouchshow.base.mvp.BasePresenterImpl;
import itbour.onetouchshow.base.mvp.BaseView;
import itbour.onetouchshow.base.mvp.MVPBaseFragment;
import itbour.onetouchshow.utils.T_;
import itbour.onetouchshow.utils.UIUtils;

/**
 * 创建人：$ gyymz1993
 * 创建时间：2017/7/21 15:14
 */
public abstract class ABaseRefreshFragment<V extends BaseView, T extends BasePresenterImpl<V>> extends MVPBaseFragment<V, T> implements BaseView {

    public static final int ON_REFRESH = 1;
    public static final int ON_LOAD = 2;
    @BindView(R.id.recyclerview)
    public RecyclerView recyclerView;
    @BindView(R.id.xrefreshview)
    public XRefreshView xRefreshView;
    @BindView(R.id.id_back_top)
    public ImageView mImageViewRebackTop;
    public int mPageIndex = 0;
    public int pullStatus;
    public BaseRefreshAdapter baseRefreshAdapter;
    private XRefreshViewFooter xRefreshViewFooter;

    @Override
    protected int getLayoutId() {
        return getChirdLayoutId();
    }

    protected abstract int getChirdLayoutId();

    @Override
    protected void initView() {
        // 设置是否可以下拉刷新
        xRefreshView.setPullRefreshEnable(true);
        // 设置是否可以上拉加载
        xRefreshView.setPullLoadEnable(true);
        // 设置上次刷新的时间
        //当下拉刷新被禁用时，调用这个方法并传入false可以不让头部被下拉
        //refreshView.setMoveHeadWhenDisablePullRefresh(true);
        // 设置时候可以自动刷新
        //xRefreshView.setAutoRefresh(true);
        xRefreshView.setAutoLoadMore(true);
        //xRefreshView.setPinnedTime(1000);
        xRefreshView.stopLoadMore(false);
        xRefreshView.setMoveForHorizontal(true);
        CustomGifHeader header = new CustomGifHeader(getContext());
        xRefreshView.setCustomHeaderView(header);
        xRefreshView.setEmptyView(R.layout.view_network_error);
        showXRefreshViewNetWorkSuccessView();
        initRecycleView();
        mImageViewRebackTop.setVisibility(View.INVISIBLE);


    }

    public void initRecycleView() {
        if (baseRefreshAdapter instanceof BaseRefreshAdapter) {
            baseRefreshAdapter = (BaseRefreshAdapter) getBaseRefreshAdapter();
        }
        if (baseRefreshAdapter == null) {
            return;
        }
        if (xRefreshViewFooter == null) {
            xRefreshViewFooter = new XRefreshViewFooter(getContext());
        }
        baseRefreshAdapter.setCustomLoadMoreView(xRefreshViewFooter);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()) {
            @Override
            public boolean canScrollVertically() {
                return true;
            }
        });
        baseRefreshAdapter.setHeaderView(getHeadView(), recyclerView);

        // 第一种，直接取消动画
        RecyclerView.ItemAnimator animator = recyclerView.getItemAnimator();
        if (animator instanceof SimpleItemAnimator) {
            ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
        }
        recyclerView.getItemAnimator().setChangeDuration(0);// 通过设置动画执行时间为0来解决闪烁问题
        recyclerView.setAdapter(baseRefreshAdapter);

    }


    public abstract RecyclerView.Adapter getBaseRefreshAdapter();

    public abstract View getHeadView();


    public void showXRefreshViewNetWorkErrorView() {
        if (pullStatus == ON_LOAD) {
            xRefreshView.stopLoadMore();
        } else if (pullStatus == ON_REFRESH) {
            xRefreshView.stopRefresh();
        }
        xRefreshView.enableEmptyView(true);
    }

    public void showXRefreshViewNetWorkSuccessView() {
        xRefreshView.enableEmptyView(false);
    }

    public void checkNetWork() {
        if (!NetUtils.isConnected(UIUtils.getContext())) {
            T_.showCustomToast("请检查网络");
            return;
        }
    }
}
